package com.quanquan.user.service.weixin;

import com.alibaba.fastjson.JSON;
import com.quanquan.user.entity.TemplateId;
import com.quanquan.user.repository.TemplateIdRepository;
import com.quanquan.user.service.UserService;
import com.quanquan.user.service.weixin.template.SendResponse;
import com.quanquan.user.service.weixin.template.TemplateData;
import com.quanquan.user.service.weixin.template.TemplateRequest;
import com.quanquan.user.service.weixin.template.WXTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.Request;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:Proxy
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/10 14:05
 * @Version 1.0
 */
@Service
public class Proxy {

    private final TemplateIdRepository templateIdRepository;
    private final UserService userService;

    @Autowired
    public Proxy(TemplateIdRepository templateIdRepository, UserService userService) {
        this.templateIdRepository = templateIdRepository;
        this.userService = userService;
    }


    /**
     * 发送 积分增加 模板
     *
     * @param templateRequest
     * @return
     */
    public WXTemplate sendIntegralSumTemplate(TemplateRequest templateRequest){
        Integer type = 1;
        List<TemplateId> templateIds =  templateIdRepository.findByType(type);
        TemplateId templateId = templateIds.get(0);

        WXTemplate wxTemplate = new WXTemplate();
        wxTemplate.setTouser(templateRequest.getOpenId());
        wxTemplate.setTemplate_id(templateId.getTemplateId());
        wxTemplate.setUrl(templateRequest.getUrl());
        Map<String, TemplateData> mapdata = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setValue(templateRequest.getDetail());
        TemplateData keyword1 = new TemplateData();
        keyword1.setValue(templateRequest.getShopName());
        TemplateData keyword2 = new TemplateData();
        keyword2.setValue(templateRequest.getMoney());
        TemplateData keyword3 = new TemplateData();
        keyword3.setValue(templateRequest.getIntegral());
        TemplateData keyword4 = new TemplateData();
        keyword4.setValue(templateRequest.getSumIntegral());
        TemplateData keyword5 = new TemplateData();
        keyword5.setValue(templateRequest.getCreateTime());
        TemplateData remark = new TemplateData();
        remark.setColor("#FF0000");
        remark.setValue(templateRequest.getRemark());

        mapdata.put("first", first);
        mapdata.put("keyword1", keyword1);
        mapdata.put("keyword2", keyword2);
        mapdata.put("keyword3", keyword3);
        mapdata.put("keyword4", keyword4);
        mapdata.put("keyword5", keyword5);
        mapdata.put("remark", remark);
        wxTemplate.setData(mapdata);
        sendTemplateMessage(wxTemplate);
        return wxTemplate;

    }

    /**
     * 发送 积分扣除 模板
     *
     * @param templateRequest
     * @return
     */
    public WXTemplate sendIntegralDelTemplate(TemplateRequest templateRequest){
        Integer type = 2;
        List<TemplateId> templateIds =  templateIdRepository.findByType(type);
        TemplateId templateId = templateIds.get(0);

        WXTemplate wxTemplate = new WXTemplate();
        wxTemplate.setTouser(templateRequest.getOpenId());
        wxTemplate.setTemplate_id(templateId.getTemplateId());
        wxTemplate.setUrl(templateRequest.getUrl());
        Map<String, TemplateData> mapdata = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setValue(templateRequest.getDetail());
        TemplateData keyword1 = new TemplateData();
        keyword1.setValue(templateRequest.getCreateTime());
        TemplateData keyword2 = new TemplateData();
        keyword2.setValue(templateRequest.getIntegral());
        TemplateData keyword3 = new TemplateData();
        keyword3.setValue(templateRequest.getReason());
        TemplateData keyword4 = new TemplateData();
        keyword4.setValue(templateRequest.getSumIntegral());
        TemplateData remark = new TemplateData();
        remark.setValue(templateRequest.getRemark());

        mapdata.put("first", first);
        mapdata.put("keyword1", keyword1);
        mapdata.put("keyword2", keyword2);
        mapdata.put("keyword3", keyword3);
        mapdata.put("keyword4", keyword4);
        mapdata.put("remark", remark);
        wxTemplate.setData(mapdata);
        sendTemplateMessage(wxTemplate);
        return wxTemplate;

    }


    //发送 推送券 模板
    public void sendPushCouponTemplate(String accessToken){
        //touser\template_id\data
    }

    /**
     * 发送模板消息
     *
     * @param requestBody
     * @return
     */
    public SendResponse sendTemplateMessage(WXTemplate requestBody) {
        String url = new StringBuffer().append("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=")
                .append(userService.getWeChatToken()).toString();
        String result = Request.postJson(url, requestBody);
        return JSON.parseObject(result, SendResponse.class);
    }


}
